using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ProjectCastamere
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Application : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        GraphicsDevice device;

        public GraphicsDeviceManager Graphics
        {
            get { return graphics; }
        }

        public enum GameStateIdentifier
        {
            //IntroState,
            MainMenuState,
            //GameSessionState
            Count
        };


        // TODO: remove these?
        public float convertX(float x)
        {
            return (x / 1920.0f) * (float)graphics.GraphicsDevice.PresentationParameters.BackBufferWidth;
        }

        public float convertY(float y)
        {
            return (y / 1080.0f) * (float)graphics.GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        private List<GameState> gameStates = new List<GameState>();

        public Application()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            Window.Title = "Epic Tower Defence";

            //graphics.PreferredBackBufferWidth = 1920;
            //graphics.PreferredBackBufferHeight = 1080;
            //graphics.IsFullScreen = true;


            device = graphics.GraphicsDevice;

            //Initialises the audio manager
            AudioManager.Initalise(this, @"Content/Audio/ProjectCastamere.xgs", @"Content/Audio/Wave Bank.xwb", @"Content\Audio\Sound Bank.xsb");
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Initialise the InputManager
            InputManager.Initialize();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;


            PushState(new MainMenu(this));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Update the InputManager
            InputManager.Update();

            // Update the correct state
            if (gameStates.Count > 0)
            {
                //query user input
                gameStates.Last().Input();

                // update the current game state
                gameStates.Last().Update();

            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

            if (gameStates.Count > 0)
            {
                gameStates.Last().Draw();
            }

            base.Draw(gameTime);
        }

        public float GetAspectRatio()
        {
            return GraphicsDevice.Viewport.AspectRatio;
        }

        public void PushState(GameState state)
        {
            if (gameStates.Count > 0)
            {
                gameStates[gameStates.Count-1].FocusLost();
            }
            gameStates.Add(state);
            state.FocusGained();
        }

        public GameState PopState()
        {
            if (gameStates.Count == 0)
            {
                return null;
            }
            GameState s = gameStates[gameStates.Count - 1];
            gameStates.RemoveAt(gameStates.Count - 1);

            if (gameStates.Count > 0)
            {
                gameStates.Last().FocusGained();
            }

            return s;
        }
    }
}
